package com.example.yan_music.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.yan_music.R;
import com.example.yan_music.bean.wangyi.RecommendSongList;
import com.example.yan_music.utils.XToast;

import java.util.List;

/**
 * <p>
 *     推荐歌单RCV适配器
 * </p>
 */
public class RcvRecommendAdapter extends RecyclerView.Adapter<RcvRecommendAdapter.XViewHolder> {

    private Context mContext;
    private List<RecommendSongList.ResultBean> mResultBeanList;
    private OnItemClickListener listener;


    public RcvRecommendAdapter(Context mContext, List<RecommendSongList.ResultBean> mResultBeanList) {
        this.mContext = mContext;
        this.mResultBeanList = mResultBeanList;
    }

    @NonNull
    @Override
    public XViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new XViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rcv_recommend_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull XViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return mResultBeanList == null ? 0 : mResultBeanList.size();
    }

    public class XViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgBac;
        private TextView tvDescription;
        private TextView tvPlayCounts;
        private int currentPos;

        public XViewHolder(@NonNull View itemView) {
            super(itemView);
            imgBac = itemView.findViewById(R.id.img_item_1);
            tvDescription = itemView.findViewById(R.id.tv_item_1);
            tvPlayCounts = itemView.findViewById(R.id.tv_visit_counts_1);

            imgBac.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(currentPos);
                }
            });
        }

        public void setData(int position){
            this.currentPos = position;
            Glide.with(mContext)
                    .load(mResultBeanList.get(position).getPicUrl())
                    .placeholder(R.drawable.pre_list_cover)
                    .into(imgBac);
            int playCounts = mResultBeanList.get(position).getPlayCount();
            if(playCounts > 9999){
                tvPlayCounts.setText((playCounts / 10000) + "万");
            } else if(playCounts > 10000000){
                tvPlayCounts.setText((playCounts / 10000000) + "亿");
            } else {
                tvPlayCounts.setText(playCounts);
            }
            tvDescription.setText(mResultBeanList.get(position).getName());
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
}
